﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAL;
using BL;

namespace QuanLyCHTrangSuc
{
    public partial class frmQuanLySanPham : Form
    {
        DanhMucLoaiSanPhamBL dmLoaiSanPhamBLL = new DanhMucLoaiSanPhamBL();
        SanPhamBL sanPhamBL = new SanPhamBL();

        List<DanhMucLoaiSanPhamDAL> dmLoaiSanPham = new List<DanhMucLoaiSanPhamDAL>();

        List<SanPhamDAL> dsSanPham;

        public frmQuanLySanPham()
        {
            InitializeComponent();
        }

        private void frmQuanLySanPham_Load(object sender, EventArgs e)
        {
            Reset();
        }

        private void Reset()
        {
            dmLoaiSanPham = dmLoaiSanPhamBLL.LayDanhMucLoaiSanPham();

            cbbNhomSanPham.DataSource = dmLoaiSanPham;
            cbbNhomSanPham.DisplayMember = "TenLoaiSanPham";
            cbbNhomSanPham.ValueMember = "MaLoaiSanPham";
        }

        private void frmQuanLySanPham_Resize(object sender, EventArgs e)
        {
            lblQuanLySanPham.Left = (int)(this.Width / 2 - lblQuanLySanPham.Width / 2);
        }

        private void cbbNhomSanPham_SelectedIndexChanged(object sender, EventArgs e)
        {
            panelDSSanPham.Controls.Clear();

            DanhMucLoaiSanPhamDAL loaiSanPham = (DanhMucLoaiSanPhamDAL)cbbNhomSanPham.SelectedItem;
            dsSanPham = sanPhamBL.LayDSSanPhamTheoMaLoai(loaiSanPham.MaLoaiSanPham);            

            this.XuatDSSPLenPanelDSSP();
        }

        private void XuatDSSPLenPanelDSSP()
        {
            for (int i = 0; i < dsSanPham.Count; ++i)
            {
                ItemUC sanPham = new ItemUC();
                sanPham.Picture.Image = Image.FromFile(dsSanPham[i].HinhAnh);
                sanPham.Label1.Text = dsSanPham[i].TenSanPham;
                sanPham.Label2.Text = dsSanPham[i].GiaBan.ToString();
                sanPham.Tag = dsSanPham[i];
                sanPham.ContextMenuStrip = SanPhamcontextMenuStrip;
                sanPham.Click += new EventHandler(sanPham_Click);

                panelDSSanPham.Controls.Add(sanPham);
            }
        }

        void sanPham_Click(object sender, EventArgs e)
        {
            ItemUC tmp;
            try
            {
                tmp = (ItemUC)sender;
            }
            catch (Exception ex)
            {
                return;
            }
            SanPhamDAL spTmp = (SanPhamDAL)tmp.Tag;
            frmThongTinChiTietSanPham frm = new frmThongTinChiTietSanPham(spTmp);
            frm.ShowDialog();
        }

        private void txtTenSanPham_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsControl(e.KeyChar))
                e.Handled = true;
            if (e.KeyChar == 13)
            {
                this.btnTimKiem_Click(sender, e);
                this.txtTenSanPham.SelectAll();
            }
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {

        }

        private void xemThongTinChiTietToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ToolStripDropDownItem menu = (ToolStripDropDownItem)sender;
            ContextMenuStrip strip = (ContextMenuStrip)menu.Owner;
            Control owner = strip.SourceControl;

            ItemUC tmp;
            try
            {
                tmp = (ItemUC)owner;
            }
            catch (Exception ex)
            {
                return;
            }
            SanPhamDAL spTmp = (SanPhamDAL)tmp.Tag;
            frmThongTinChiTietSanPham frm = new frmThongTinChiTietSanPham(spTmp);
            frm.ShowDialog();
        }

        private void XoaSanPhamToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ToolStripDropDownItem menu = (ToolStripDropDownItem)sender;
            ContextMenuStrip strip = (ContextMenuStrip)menu.Owner;
            Control owner = strip.SourceControl;

            ItemUC tmp;
            try
            {
                tmp = (ItemUC)owner;
            }
            catch (Exception ex)
            {
                return;
            }
            SanPhamDAL sanPham = (SanPhamDAL)tmp.Tag;

            DialogResult kq = MessageBox.Show("Bạn thực sự muốn xóa sản phẩm" + sanPham.TenSanPham + "?",
                "Nhắc nhở",
                MessageBoxButtons.YesNoCancel,
                MessageBoxIcon.Question,
                MessageBoxDefaultButton.Button3);

            if (kq == DialogResult.Yes)
            {
                if (sanPhamBL.CapNhatTinhTrangSanPham(sanPham))
                {
                    MessageBox.Show("Xóa thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Có lỗi xảy ra trong quá trình ghi dữ liệu.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else if (kq == DialogResult.No)
            {
                return;
            }
            else if (kq == DialogResult.Cancel)
            {
                return;
            }
            this.Reset();
        }

        private void capNhatSanPhamToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ToolStripDropDownItem menu = (ToolStripDropDownItem)sender;
            ContextMenuStrip strip = (ContextMenuStrip)menu.Owner;
            Control owner = strip.SourceControl;

            ItemUC tmp;
            try
            {
                tmp = (ItemUC)owner;
            }
            catch (Exception ex)
            {
                return;
            }
            SanPhamDAL spTmp = (SanPhamDAL)tmp.Tag;
            frmCapNhatSanPham frm = new frmCapNhatSanPham(spTmp); 
            frm.ShowDialog();
            DanhMucLoaiSanPhamDAL loaiSanPham = (DanhMucLoaiSanPhamDAL)cbbNhomSanPham.SelectedItem;
            this.Reset();
            cbbNhomSanPham.SelectedItem = loaiSanPham;
        }

        private void themSanPhamMoiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmThemMoiSanPham frm = new frmThemMoiSanPham();
            frm.ShowDialog();
            //DanhMucLoaiSanPhamDAL loaiSanPham = (DanhMucLoaiSanPhamDAL)cbbNhomSanPham.SelectedItem;
            //this.Reset();
            //cbbNhomSanPham.SelectedItem = loaiSanPham;
        }
    }
}
